import { INIT, SET_TOKEN, SET_USER_INFO } from 'src/redux/actions/actionTypes'
import { Action } from 'src/types/action'
import { User } from 'src/common/storage'

interface UserState {
    token: string | null
    user: any
}

// 初始化 state
const userState: UserState = {
    token: User.getToken(),
    user: {}
}

export default function reducer(state = userState, action: Action): UserState {
    switch (action.type) {
        case INIT:
            return { ...state }
        case SET_TOKEN:
            return { ...state, token: action.data }
        case SET_USER_INFO:
            return { ...state, user: action.data }
        default:
            return state
    }
}
